package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.my.MessageListAdapter;
import com.shbaiche.bifenba.adapter.my.MyCommentListAdapter;
import com.shbaiche.bifenba.model.GankResults;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.FavariteMessage;
import com.shbaiche.bifenba.model.user.MyDiscussMessage;
import com.shbaiche.bifenba.model.video.ArticleDiscussVo;
import com.shbaiche.bifenba.model.video.ArticleGroupVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.ui.base.BasePagerActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroidbase.router.Router;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;


/**
 * Created by wanglei on 2016/12/9.
 */

public class MyCommentListActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    MyCommentListAdapter adapter;

    protected static final int PAGE_SIZE = 10;
    protected static final int MAX_PAGE = 10;

    Dialog dg;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if (App.getPassport() == null) {
            ToastUtil.show("请先登录!");
            finish();
            return;
        }
        initAdapter();
        loadData(0);
    }

    @Override
    public void setToolbar() {
        initToolbar(toolbar, setTitle());
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData(1);
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData(page);
                    }
                });

        contentLayout.loadingView(View.inflate(this, R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
    }

    public void loadData(final int page) {

        dg = new Dialog(this);
        dg.setContentView(R.layout.view_loading);
        dg.show();

        MyDiscussMessage msg = new MyDiscussMessage();
        msg.setPassport(App.getPassport());
        msg.setPageSize(PAGE_SIZE);
        msg.setPageIndex(page);
        NetApi.MyDiscuss(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                MyDiscussMessage msg = gson.fromJson(response.getMessage(), MyDiscussMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    if (page > 1) {
                        getAdapter().addData(msg.getVoList());
                    } else {
                        getAdapter().setData(msg.getVoList());
                    }

                    contentLayout.getRecyclerView().setPage(page, MAX_PAGE);

                    if (getAdapter().getItemCount() < 1) {
                        contentLayout.showEmpty();
                        return;
                    }
                } else {
                }
            }
        });


    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_base_pager;
    }


    public String setTitle() {
        return "我的评论";
    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new MyCommentListAdapter(context);
//            adapter.setRecItemClick(new RecyclerItemCallback<ArticleDiscussVo, MyCommentListAdapter.ViewHolder>() {
//                @Override
//                public void onItemClick(int position, ArticleDiscussVo model, int tag, MyCommentListAdapter.ViewHolder holder) {
//                    super.onItemClick(position, model, tag, holder);
//                }
//            });
        }
        return adapter;
    }

    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(this);
    }

    public String getType() {
        return "福利";
    }


    public static void launch(Activity activity) {
        Router.newIntent()
                .from(activity)
                .to(MyCommentListActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }


}
